/*
 * Copyright 2015-2016 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * You may not use this file except in compliance with the terms and conditions set forth
 * in the accompanying LICENSE.TXT file.
 *
 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS.  AMAZON SPECIFICALLY DISCLAIMS, WITH
 * RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 */
package com.amazon.alexa.voicechrome.internal.timer;

import java.util.Timer;
import java.util.TimerTask;

public class UpdateTimer implements SpeechUiTimer {

    public static final long TIMER_RATE = 75;
    private TimerTickCallback callback;
    private final Timer timer;

    public UpdateTimer(TimerTickCallback callback){
        this(callback, new Timer());
    }

    protected UpdateTimer(TimerTickCallback callback, Timer timer) {
        this.callback = callback;
        this.timer = timer;
    }

    @Override
    public void start() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                callback.update();
            }
        }, TIMER_RATE, TIMER_RATE);
    }

    @Override
    public void stop() {
        timer.cancel();
        timer.purge();
    }
}
